
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	"log"
	"time"
)

type CrconfigSnapshots struct {
	Snapshot  string                 `db:"snapshot" json:"snapshot"`
	CreatedAt time.Time              `db:"created_at" json:"createdAt"`
	Links     CrconfigSnapshotsLinks `json:"_links" db:-`
}

type CrconfigSnapshotsLinks struct {
	Self     string   `db:"self" json:"_self"`
	CdnsLink CdnsLink `json:"cdns" db:-`
}

// @Title getCrconfigSnapshotsById
// @Description retrieves the crconfig_snapshots information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    CrconfigSnapshots
// @Resource /api/2.0
// @Router /api/2.0/crconfig_snapshots/{id} [get]
func getCrconfigSnapshot(cdn string, createdAt time.Time, db *sqlx.DB) (interface{}, error) {
	ret := []CrconfigSnapshots{}
	arg := CrconfigSnapshots{}
	arg.Links.CdnsLink.ID = cdn
	arg.CreatedAt = createdAt
	queryStr := "select *, concat('" + API_PATH + "crconfig_snapshots', '/cdn/', cdn, '/created_at/', created_at) as self"
	queryStr += ", concat('" + API_PATH + "cdns/', cdn) as cdns_name_ref"
	queryStr += " from crconfig_snapshots WHERE cdn=:Links.CdnsLink.ID AND created_at=:created_at"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getCrconfigSnapshotss
// @Description retrieves the crconfig_snapshots
// @Accept  application/json
// @Success 200 {array}    CrconfigSnapshots
// @Resource /api/2.0
// @Router /api/2.0/crconfig_snapshots [get]
func getCrconfigSnapshots(db *sqlx.DB) (interface{}, error) {
	ret := []CrconfigSnapshots{}
	queryStr := "select *, concat('" + API_PATH + "crconfig_snapshots', '/cdn/', cdn, '/created_at/', created_at) as self"
	queryStr += ", concat('" + API_PATH + "cdns/', cdn) as cdns_name_ref"
	queryStr += " from crconfig_snapshots"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postCrconfigSnapshots
// @Description enter a new crconfig_snapshots
// @Accept  application/json
// @Param                 Body body     CrconfigSnapshots   true "CrconfigSnapshots object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/crconfig_snapshots [post]
func postCrconfigSnapshot(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v CrconfigSnapshots
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO crconfig_snapshots("
	sqlString += "cdn"
	sqlString += ",snapshot"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":cdn"
	sqlString += ",:snapshot"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putCrconfigSnapshots
// @Description modify an existing crconfig_snapshotsentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     CrconfigSnapshots   true "CrconfigSnapshots object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/crconfig_snapshots/{id}  [put]
func putCrconfigSnapshot(cdn string, createdAt time.Time, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg CrconfigSnapshots
	err := json.Unmarshal(payload, &arg)
	arg.Links.CdnsLink.ID = cdn
	arg.CreatedAt = createdAt
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE crconfig_snapshots SET "
	sqlString += "cdn = :cdn"
	sqlString += ",snapshot = :snapshot"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE cdn=:Links.CdnsLink.ID AND created_at=:created_at"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delCrconfigSnapshotsById
// @Description deletes crconfig_snapshots information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    CrconfigSnapshots
// @Resource /api/2.0
// @Router /api/2.0/crconfig_snapshots/{id} [delete]
func delCrconfigSnapshot(cdn string, createdAt time.Time, db *sqlx.DB) (interface{}, error) {
	arg := CrconfigSnapshots{}
	arg.Links.CdnsLink.ID = cdn
	arg.CreatedAt = createdAt
	result, err := db.NamedExec("DELETE FROM crconfig_snapshots WHERE cdn=:Links.CdnsLink.ID AND created_at=:created_at", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
